<?php


class dbaCMSLang {

    function dbaCMSLang(&$dba)
    {
        $this->dba = &$dba;
    }

    function getLanguages(&$data,$ordered = false)
    {
    	$orderStr = '';
        if ($ordered)
        {
            $orderStr = ' order by name';
        }
        $check = $this->dba->query("select * from ".$this->dba->tableforwarder."_cmsLang$orderStr;");
        $data  = $this->dba->fetch_rows_array();
        return $check;
   }

    /**
     * Get lang id for lang name
     * @param string $name Lang name (e.g. de, en)
     * @return int Lang id
     */
    function getLangID($name)
    {
        $this->dba->query("select id from ".$this->dba->tableforwarder."_cmsLang where name="
            .$this->dba->quote($name).";");
        $row = $this->dba->fetch_row_array();
        return $row['id'];
    }

    function getLanguage(&$result,$id)
    {
        $check  = $this->dba->query("select * from ".$this->dba->tableforwarder."_cmsLang where id = $id;");
        $result = $this->dba->fetch_row_array();
        return $check;
    }

    function delLanguage($id)
    {
        return $this->dba->query("delete from ".$this->dba->tableforwarder."_cmsLang where id = $id;");
    }

    function updateLanguage($id,$name)
    {
        return $this->dba->query("update ".$this->dba->tableforwarder."_cmsLang set name="
            .$this->dba->quote($name)." where id = $id;");
    }

    function addLanguage($name)
    {
        return $this->dba->query("insert into ".$this->dba->tableforwarder."_cmsLang (name) values ("
            .$this->dba->quote($name).")");
    }
   
    function isPublishedInLang ($doc_id,$lang_id)
    {
        $sth = $this->dba->query("select lang_id from ".$this->dba->tableforwarder
            ."_cmsPubLang where doc_id=$doc_id and lang_id=$lang_id;");
        if ($this->dba->num_rows())
        {
            return true;
        }
    }
    

    function getPubLangByDoc ($doc_id)
    {
        $sth = $this->dba->query("select lang_id from ".$this->dba->tableforwarder."_cmsPubLang where doc_id=$doc_id;");
        if (is_object($sth))
        {
        	$lang = array();
            $rows = $sth->fetch_rows();
            for ($i=0;$i<count($rows);$i++)
            {
                $lang[] = $rows[$i][0];
            }
            return $lang;
        }
    }
    
    function getPubLangNamesByDoc ($doc_id)
    {
        $sth = $this->dba->query("select name from ".$this->dba->tableforwarder."_cmsPubLang left join "
            .$this->dba->tableforwarder."_cmsLang on (id=lang_id) where doc_id=$doc_id order by name;");
        if (is_object($sth))
        {
        	$lang = array();
            $rows = $sth->fetch_rows();
            for ($i=0;$i<count($rows);$i++)
            {
                $lang[] = $rows[$i][0];
            }
            return $lang;
        }
    }
    
    function deletePubLangByDoc ($doc_id)
    {
        return $this->dba->query("delete from ".$this->dba->tableforwarder."_cmsPubLang where doc_id=$doc_id;");
    }
    
    function setPubLangByDoc ($doc_id,$langcsv)
    {
        $lang = explode(';',$langcsv);
        if (is_array($lang))
        {
            $query = '';
            foreach($lang As $l)
            {
                $query .= "($doc_id,$l),";
            }
            $this->deletePubLangByDoc($doc_id);
            return $this->dba->query("insert into ".$this->dba->tableforwarder."_cmsPubLang (doc_id,lang_id) values ".preg_replace("/,$/",';',$query));
        }
    }
}
?>